package org.komarichin.jdo.dao;

import org.apache.commons.collections.CollectionUtils;
import org.komarichin.beans.IRole;
import org.komarichin.beans.dao.IRoleDAO;
import org.komarichin.jdo.persistence.Role;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.jdo.support.JdoDaoSupport;
import org.springframework.transaction.annotation.Transactional;

import javax.jdo.JDOHelper;
import java.util.Collection;

/**
 * User: Michael Komarichin
 * Date: 05.10.2009
 * Time: 10:33:40
 */
@Transactional(readOnly = true)
public class RoleDAO extends JdoDaoSupport implements IRoleDAO {
    private static final long serialVersionUID = 7025767201299050183L;
    private static final Logger log = LoggerFactory.getLogger(RoleDAO.class);

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(readOnly = false)
    public IRole save(IRole role) {
        log.debug("save#role : {}", role);
        return (IRole) getJdoTemplate().makePersistent(role);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(readOnly = false)
    public void remove(IRole role) {
        log.debug("remove#role : {}", role);
        getJdoTemplate().deletePersistent(JDOHelper.getObjectId(role.getId()));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @Transactional(readOnly = false)
    public void update(IRole role) {
        log.debug("update#role : {}", role);
        getJdoTemplate().makePersistent(role);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IRole getRoleByName(String name) {
        log.debug("getRoleByName#name : {}", name);
        Collection result = getJdoTemplate().find(Role.class, "name == value", "String value", new String[]{name});
        log.debug("result: {}", result);
        return result != null && result.size() > 0 ? (Role) CollectionUtils.get(result, 0) : null;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IRole getRoleById(Long id) {
        log.debug("getRoleById#id : {}", id);
        IRole role = (IRole) getJdoTemplate().getObjectById(Role.class, id);
        log.debug("result: {}", role);
        return role;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @SuppressWarnings("unchecked")
    public Collection<IRole> getAll() {
        log.debug("getAll");
        Collection<IRole> result = getJdoTemplate().find(Role.class);
        log.debug("result : {}", result);
        return result;
    }

    @Override
    @SuppressWarnings("unchecked")
    public Collection<? extends IRole> getAllowRoles(Long permission) {
        log.debug("getAllowRoles#permission : {}", permission);
        Collection<IRole> result = getJdoTemplate().find(Role.class, "permission >= pPermission",
                "Long pPermission", new Object[]{permission});
        log.debug("result : {}", result);
        return result;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public IRole newInstanceRole(Long id, String name, Long permission) {
        log.debug("newInstanceRole#id : {}, name : {}, permission : {}", new Object[]{id, name, permission});
        return new Role(id, name, permission);
    }
}
